package com.jbit.dm_160.controller;

import com.jbit.dm_160.model.entity.LinkUser;
import com.jbit.dm_160.model.entity.User;
import com.jbit.dm_160.service.LinkUserService;
import com.jbit.dm_160.service.UserService;
import com.jbit.dm_160.utils.RedisUtils;
import com.jbit.dm_160.utils.ResponseEntity;
import com.jbit.dm_160.utils.ResponseUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user/api")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private LinkUserService linkUserService;
    @PostMapping("/p/login")
    public ResponseEntity<Object[]> login(@RequestBody User user){
        Object[] objects = userService.getUserByPhone(user.getPhone(),user.getPassword());
        if (objects == null){
            return ResponseUtils.sendError("1006","账号或密码错误");
        }else{
            return ResponseUtils.sendSuccess(objects);
        }
    }
    @PostMapping("/v/queryLinkUser")
    public ResponseEntity<List<LinkUser>> queryLinkUser(@RequestHeader String token){
        User user = userService.getUserByToken(token);
        List<LinkUser> linkUserList = linkUserService.getLinkUsersByUserId(user.getId());
        return ResponseUtils.sendSuccess(linkUserList);
    }

    @PostMapping("/v/queryLinkUserByIdCard")
    public ResponseEntity queryLinkUserByIdCard(@RequestHeader String token,@RequestBody LinkUser linkUser){
        User user = userService.getUserByToken(token);
        linkUser.setUserId(user.getId());
        boolean flag = linkUserService.existsLinkUser(linkUser);
        return flag ? ResponseUtils.sendSuccess(null) : ResponseUtils.sendError("1009","购票人已存在");
    }
    @PostMapping("/v/addLinkUser")
    public ResponseEntity addLinkUser(@RequestBody LinkUser linkUser,@RequestHeader String token){
        User user = userService.getUserByToken(token);
        linkUser.setUserId(user.getId());

        Integer count = linkUserService.addLinkUser(linkUser);
        return ResponseUtils.sendSuccess(null);
    }


}
